package org.wenhuayuan.library.log;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import org.wenhuayuan.library.R;

import java.util.ArrayList;
import java.util.List;

public class HiViewPrinter implements HiLogPrinter {
    private RecyclerView recyclerView;
    private LogAdapter logAdapter;
    private HiViewPrinterProvider viewProvider;

    public HiViewPrinter(Activity aty) {
        FrameLayout rootView = aty.findViewById(android.R.id.content);
        this.recyclerView = new RecyclerView(aty);
        logAdapter = new LogAdapter(LayoutInflater.from(aty));
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(aty);
        this.recyclerView.setLayoutManager(linearLayoutManager);
        this.recyclerView.setAdapter(logAdapter);

        viewProvider = new HiViewPrinterProvider(rootView, this.recyclerView);//初始化
    }
    @Override
    public void print(@NonNull HiLogConfig config, int level, String tag, @NonNull String printString) {
        // 将日志内容添加到RecyclerView中，并显示到日志窗口
        this.logAdapter.addItem(new HiLogMo(System.currentTimeMillis(), level, tag, printString));
        this.recyclerView.smoothScrollToPosition(this.logAdapter.getItemCount() - 1);
    }

    /**
     * 获取 viewProvider，通过viewProvider可以控制log视图
     * @return
     */
    public HiViewPrinterProvider getViewProvider() {
        return viewProvider;
    }

    private static class LogAdapter extends RecyclerView.Adapter<LogAdapter.LogViewHolder> {
        private LayoutInflater inflater;
        private List<HiLogMo> logs = new ArrayList<>();
        public LogAdapter(LayoutInflater inflater) {
            this.inflater = inflater;
        }
        protected void addItem(HiLogMo mo) {
            logs.add(mo);
            notifyItemInserted(logs.size() - 1);
        }
        @NonNull
        @Override
        public LogViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View itemView = inflater.inflate(R.layout.hilog_item, parent, false);
            return new LogViewHolder(itemView);
        }

        @Override
        public void onBindViewHolder(@NonNull LogViewHolder holder, int position) {
            HiLogMo logItem = logs.get(position);
            int color = getHighlightColor(logItem.level);
            holder.tagView.setTextColor(color);
            holder.tagView.setText(logItem.getFlattened());
            holder.messageView.setTextColor(color);
            holder.messageView.setText(logItem.log);
        }

        @Override
        public int getItemCount() {
            return logs.size();
        }

        /**
         * 通过log级别设置不同字体颜色
         *
         * @param logLevel log 级别
         * @return 高亮的颜色
         */
        private int getHighlightColor(int logLevel) {
            int highlight;
            switch (logLevel) {
                case HiLogType.V:
                    highlight = 0xffbbbbbb;
                    break;
                case HiLogType.D:
                    highlight = 0xffffffff;
                    break;
                case HiLogType.I:
                    highlight = 0xff6a8759;
                    break;
                case HiLogType.W:
                    highlight = 0xffbbb529;
                    break;
                case HiLogType.E:
                    highlight = 0xffff6b68;
                    break;
                default:
                    highlight = 0xffffff00;
                    break;
            }
            return highlight;
        }

        private static class LogViewHolder extends RecyclerView.ViewHolder {
            private TextView tagView;
            private TextView messageView;
            public LogViewHolder(@NonNull View itemView) {
                super(itemView);

                tagView = itemView.findViewById(R.id.tag);
                messageView = itemView.findViewById(R.id.message);
            }
        }
    }


}
